<?php

/**
 * This is the model class for table "listmusic".
 *
 * The followings are the available columns in table 'listmusic':
 * @property integer $id
 * @property integer $user_id
 * @property string $title
 * @property string $posttime
 * @property string $content
 * @property string $photo
 * @property integer $like
 * @property integer $feed
 * @property string $pricavy
 * @property string $other
 *
 * The followings are the available model relations:
 * @property Comment[] $comments
 * @property ListRelationsMusic[] $listRelationsMusics
 * @property User $user
 * @property Tag[] $tags
 */
class Listmusic extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'listmusic';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_id, title, posttime, content, pricavy', 'required'),
			array('user_id, like, feed', 'numerical', 'integerOnly'=>true),
			array('title', 'length', 'max'=>100),
			array('photo, pricavy', 'length', 'max'=>32),
			array('other', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, user_id, title, posttime, content, photo, like, feed, pricavy, other', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'comments' => array(self::HAS_MANY, 'Comment', 'list_id'),
			'listRelationsMusics' => array(self::HAS_MANY, 'ListRelationsMusic', 'list_id'),
			'user' => array(self::BELONGS_TO, 'User', 'user_id'),
			'tags' => array(self::HAS_MANY, 'Tag', 'list_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'user_id' => 'User',
			'title' => 'Title',
			'posttime' => 'Posttime',
			'content' => 'Content',
			'photo' => 'Photo',
			'like' => 'Like',
			'feed' => 'Feed',
			'pricavy' => 'Pricavy',
			'other' => 'Other',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('posttime',$this->posttime,true);
		$criteria->compare('content',$this->content,true);
		$criteria->compare('photo',$this->photo,true);
		$criteria->compare('like',$this->like);
		$criteria->compare('feed',$this->feed);
		$criteria->compare('pricavy',$this->pricavy,true);
		$criteria->compare('other',$this->other,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Listmusic the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public function add()
	{
		$this->attributes = $_POST['Listmusic'];
		return $this->save();
	}

	public function view($listmusic_id)
	{
		return $this->findByPk($listmusic_id)->attributes;
	}

	public function viewMusic($listmusic_id)
	{
		$sql = "
				SELECT
				list_relations_music.id,
				list_relations_music.list_id,
				list_relations_music.music_id,
				music.title,
				`user`.`name`,
				music.link
				FROM
				music
				INNER JOIN list_relations_music ON music.id = list_relations_music.music_id
				INNER JOIN `user` ON `user`.id = music.user_id
				WHERE
				list_relations_music.list_id = $listmusic_id
		";
		$query=Yii::app()->db->createCommand($sql)->queryAll();
		return $query;
	}

	public function remove($listmusic_id)
	{
		return $this->deleteByPk($listmusic_id);
	}

	public function edit($listmusic_id)
	{
		$ob = $this->findByPk($listmusic_id);
		$ob->attributes=$_POST['Listmusic'];
		return $ob->save();
	}

	public function getList($user_id)
	{
		$Criteria = new CDbCriteria();
		$Criteria->condition = "user_id = $user_id";
		$data = $this->findAll($Criteria);
		return $data;
	}
	public function getList5($user_id,$relation)
	{
		$Criteria = new CDbCriteria();
		switch ($relation) {
			case 'me':
				$Criteria->condition = "user_id = $user_id";
				break;
			case 'none':
				$Criteria->condition = "user_id = $user_id AND pricavy = ".'"public"';
				break;
			case 'friend':
				$Criteria->condition = "user_id = $user_id AND pricavy != ".'"private"';
				break;
		}
		//$Criteria->condition = "user_id = $user_id AND user_id = $user_id";
		$Criteria->limit = 5;
		$data = $this->findAll($Criteria);
		return $data;
	}

	public function getListFavorite()
	{
		$Criteria = new CDbCriteria();
		$Criteria->condition = "pricavy = ".'"public" ';
		$Criteria->order = '"like" ASC';
		$Criteria->limit = 4;
		$data = $this->findAll($Criteria);
		return $data;
	}

	public function getListRandom()
	{
		$Criteria = new CDbCriteria();
		$Criteria->condition = "pricavy = ".'"public" ';
		$Criteria->order = 'rand()';
		$Criteria->limit = 4;
		$data = $this->findAll($Criteria);
		return $data;
	}
}
